Improve compiler detection for __builtin_popcount()
authorEmmanuele Bassi <ebassi@gnome.org>
Wed, 23 Sep 2015 10:01:46 +0000 (11:01 +0100)
committerEmmanuele Bassi <ebassi@gnome.org>
Wed, 23 Sep 2015 12:58:50 +0000 (13:58 +0100)
The popcount builtin was added in GCC after version 4.2 (which is what
some *BSDs are using), which means we need to be more specific when
using it than just asking for GCC.

While we're at it, we can improve the compiler detection, and use a
builtin popcount on Clang ≥ 3.1 and MSVC 2008.

https://bugzilla.gnome.org/show_bug.cgi?id=755455

gtk/gtkcssselector.c

index 66ab9368864c32f355516de3e7048f687f21664b..f252bbc7b6c1f9a671dc3cc315b3673c71e93ad6 100644 (file)
 #include "gtkcssprovider.h"
 #include "gtkstylecontextprivate.h"
 
+#if defined(_MSC_VER) && _MSC_VER > 1500
+# include <intrin.h>
+# define __builtin_popcount(n) __popcount(n)
+#endif
+
 typedef struct _GtkCssSelectorClass GtkCssSelectorClass;
 typedef gboolean (* GtkCssSelectorForeachFunc) (const GtkCssSelector *selector,
                                                 const GtkCssMatcher  *matcher,
@@ -749,7 +754,11 @@ gtk_css_selector_region_get_change (const GtkCssSelector *selector, GtkCssChange
 static inline guint
 count_bits (guint n)
 {
-#if defined(__GNUC__)
+#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
+  return (guint) __builtin_popcount (n);
+#elif defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 1))
+  return (guint) __builtin_popcount (n);
+#elif defined(_MSC_VER) && _MSC_VER > 1500
   return (guint) __builtin_popcount (n);
 #else
   guint result = 0;